<?
/*
 * C 端下单文件，例如：云顶、二级域名下单类
 * author.TongBC
 *下单基类
 *通过调用order()完成下单流程
 */
class OrderSubmitC extends OrderSubmit{
    
    public function __construct(SoapClient $s, ProductInfo $p, Member $m,$ordermode, $sale){
        parent::__construct($s,$p,$m,$ordermode,$sale);
    }
    
    // 下单前检验
    public function beforeCheck($option){
        

        
        $s=$this->s;
        $this->option = $option;
        $member=$this->m->memberID;
        $pid   = $this->p->pid;
        $begintime=$option['begintime'];
        $ordername=$option['ordername'];
        $ordertel=$option['ordertel'];
        $contacttel=$option['contacttel'];
        $tnum=$option['tnum'];
        $remoteorder=$option['remoteorder'];
        $paymode=$option['paymode'];
        //TODO::订单类型，normalOrder=>普通订单，linkOrder=>联票订单，packageOrder=>套票订单
        $orderType = 'normalOrder';
        $p_info=$this->p->pInfo();
        // print_r($p_info);
        $endtime = ($p_info['delaydays']-1)<0 ? 0 :($p_info['delaydays']-1);
        $time        = strtotime($begintime)+$endtime*24*3600;
        $endtime     = date("Y-m-d",$time);
        $begintime  .=" 00:00:00";
        $endtime    .=" 23:59:59";

        /*产品预订检查*/
        $totalMoney = 0;
        $account = $option['account'] ? $option['account']:'';
        $tprice=$this->p->tPrice_n(1,$begintime,$aid,'',$account);// 新接口获取
        // echo $tprice;
        // exit;
        if($tprice['status']=='fail') throw new Exception($tprice['msg']);
        $totalTprice=$tprice*$tnum;
        $totalMoney+=$totalTprice;
        $smsSend=0;
        $reSendMsg=1;
        $pcode=0;
        $ordermode=$this->ordermode;
        // $ordermode=0;
        if($paymode==1){
            $smsSend=1;
            $reSendMsg=0;
        }
        if($p_info['p_type']=="B"){/*线路产品额外信息*/
            $playdateStr=explode("-",substr($begintime,0,10));
            $playdateStr=substr($playdateStr[0],2,2).$playdateStr[1].$playdateStr[2];
            $series=preg_replace('/{fck_date}/',$playdateStr,$p_info['series_model']);         //团号
            $assembly=abs($_REQUEST['assembly']);//集合地点
        }
        $proInfos=array();
        $c_infos=array();
        if($p_info['p_type']=="C"){//酒店按联票产品下单
            $leavetime=$option['leavetime'];
            $days=ceil((strtotime($leavetime)-strtotime($begintime))/24/3600);
            $orderType = $days>1 ? 'linkOrder' : 'normalOrder';
            $cpro=new ProductInfo($s, $p_info['pid'], $this->p->aid);
            for($n=1;$n<$days;$n++){
                try{
                    $date=date("Y-m-d",strtotime($begintime)+3600*24*$n);
                    $starttime = $date." 00:00:00";
                    $c_tprice = $this->p->tPrice_n(1,$starttime, $aid, $c_pid,$account);// 新接口
                    $c_tnum=$tnum;
                    $endtime=$date." 23:59:59";
                    $c_infos[]=array("member"=>$member,
                        "tprice"=>$c_tprice,"tnum"=>$c_tnum,
                        "ltitle"=>$p_info['ltitle'],
                        "ttitle"=>$p_info['ttitle'],
                        "pid"=>$p_info['pid'],
                        "lid"=>$p_info['lid'],
                        "tid"=>$p_info['tid'],
                        "begintime"=>$starttime,
                        "endtime"=>$date." 23:59:59",
                        "storage"=>$p_info['storage'],
                        "storage_open"=>$p_info['storage_open'],
                        "salerid"=>$p_info['salerid'],
                        "ordername"=>$ordername,
                        "ordertel"=>$ordertel,
                        "paymode"=>$paymode,
                        "ordermode"=>$ordermode,
                        "apply_did"=>$p_info['apply_did'],
                        "sapply_did"=>$p_info['sapply_did'],
                        "contacttel"=>$option['contacttel'],
                        "idCard"=>$option['idCard']
                    );
                    $totalMoney+=$c_tprice * $c_tnum;
                }catch(Exception $e){
                }
            }
        }
        elseif($p_info['p_type']=="F"){//套票产品
            $pcode = 1;
            $orderType = 'packageOrder';
            $bt = substr($begintime, 0 , 10);
            $aid = $p_info['sapply_did'];
            $chk_ret = \pft\Prod\Sale::PackageTicketCheck($s, $pid, $member, $aid,$bt, $tnum);
            if ($chk_ret['code']!==200) {
                
                $msg = $chk_ret['code']> 908 ? "子票【{$chk_ret['pid']}】" : '';
                throw new DisOrderException( $msg . $chk_ret['msg'] );
            }
        }
        else{
            /*查看是否存在联票产品，存在的话记录联票产品下单信息*/
            foreach((array)$option['c_pids'] as $c_pid=>$c_tnum){
                if($c_tnum>0){
                    $orderType = 'linkOrder';
                    $c_pid=abs($c_pid);
                    $c_about  = $this->p->pInfo($c_pid);
                    $c_tprice = $this->p->tPrice_n(1,$begintime, $aid, $c_pid,$account);// 新接口
                    $c_infos[]=array(
                        "member"=>$member,
                        "tprice"=>$c_tprice,
                        "tnum"=>$c_tnum,
                        "ltitle"=>$c_about['ltitle'],
                        "ttitle"=>$c_about['ttitle'],
                        "pid"=>$c_pid,
                        "lid"=>$c_about['lid'],
                        "tid"=>$c_about['tid'],
                        "begintime"=>$begintime,
                        "endtime"=>$endtime,
                        "storage"=>$c_about['storage'],
                        "storage_open"=>$c_about['storage_open'],
                        "salerid"=>$c_about['salerid'],
                        "ordername"=>$ordername,
                        "ordertel"=>$ordertel,
                        "paymode"=>$paymode,
                        "ordermode"=>$ordermode,
                        "apply_did"=>$c_about['apply_did'],
                        "sapply_did"=>$c_about['sapply_did'],
                        "contacttel"=>$option['contacttel'],
                        "idCard"=>$option['idCard']
                    );
                    $totalMoney+=$c_tprice*$c_tnum;
                }
            }
        }
        $sendVoucher = $p_info['sendVoucher'];
        $sendVoucher_s = $p_info['sendVoucher'];
        if($paymode==1){
            $sendVoucher = 1;
            $sendVoucher_s = 1;
        }
        if(count($c_infos)>0){//联票购买
            $smsSend=1;
            $proInfos=$c_infos;
            $sendVoucher = 1;
        }

        $proInfos[]=array(
            "member"=>$member,
            "tprice"=>$tprice,
            "tnum"=>$tnum,
            "ltitle"=>$p_info['ltitle'],
            "ttitle"=>$p_info['ttitle'],
            "pid"=>$p_info['pid'],
            "tid"=>$p_info['tid'],
            "begintime"=>$begintime,
            "endtime"=>$endtime,
            "storage"=>$p_info['storage'],
            "storage_open"=>$p_info['storage_open'],
            "sapply_did"=>$p_info['sapply_did'],
            "idCard"=>$option['idCard']
        );

        $this->orderCheck($option,$p_info); //预订判定
        //TODO::如果是零售（散客购买),就不判断购买权限，反正都是用零售价
		// if($_SESSION['sid']==5338){
			// print_r($proInfos);
			// echo $p_info['sapply_did'];
		// }
        // 
        // 
        // exit;
        $this->authCheck($proInfos,$p_info['sapply_did']);        //购买权限判定
        $this->storageCheck($proInfos);     //库存判定
        $this->payCheck($totalMoney);	    //金额判定
        
        // 保存需要的数据
        $data['main']= array(
            'member'=>$member,
            'lid'=>$p_info['lid'],
            'tid'=>$p_info['tid'],
            'begintime'=>$begintime,
            'endtime'=>$endtime,
            'tprice'=>$tprice,
            'tnum'=>$tnum,
            'totalTprice'=>$totalTprice,
            'ordername'=>$ordername,
            'ordertel'=>$ordertel,
            'contacttel'=>$contacttel,
            'salerid'=>$p_info['salerid'],
            'sapply_did'=>$option['sapply_did'],
            'paymode'=>$paymode,
            'ordermode'=>$ordermode,
            'remoteorder'=>$remoteorder,
            'assembly'=>$assembly,
            'series'=>$series,
            'smsSend'=>$smsSend,
            'pcode'=>$pcode,
            'idCard'=>$option['idCard']
        );
        
        // 其他联票套票酒店下单数据
        $data['option'] = $option;   // 记录订单信息
        $data['smsSend'] = $smsSend; // 是否发送短信
        $data['c_infos'] = $c_infos; // 联票信息
        $data['p_type'] = $p_info['p_type'];
        $data['reSendMsg'] = $reSendMsg;
		return $data;
		
    }
    
    public function createOrder($data,$s){
        // echo "Group_Order_Submit({$data['main']['member']},{$data['main']['lid']},{$data['main']['tid']},{$data['main']['begintime']},{$data['main']['endtime']},{$data['main']['tprice']},{$data['main']['tnum']},{$data['main']['totalTprice']},{$data['main']['ordername']},{$data['main']['ordertel']},{$data['main']['contacttel']},{$data['main']['salerid']},{$data['main']['sapply_did']},{$data['main']['paymode']},{$data['main']['ordermode']},{$data['main']['remoteorder']},{$data['main']['assembly']},{$data['main']['series']},{$data['main']['smsSend']},0,{$data['main']['pcode']},{$data['main']['idCard']})";
        // exit;
        $r=$s->Group_Order_Submit(
            $data['main']['member'], // $data['main']['sapply_did'],
            $data['main']['lid'],
            $data['main']['tid'],
            $data['main']['begintime'],
            $data['main']['endtime'],
            $data['main']['tprice'],
            $data['main']['tnum'],
            $data['main']['totalTprice'],
            $data['main']['ordername'],
            $data['main']['ordertel'],
            $data['main']['contacttel'],
            $data['main']['salerid'],
            $data['main']['sapply_did'],
            $data['main']['paymode'],
            $data['main']['ordermode'],
            $data['main']['remoteorder'],
            $data['main']['assembly'],
            $data['main']['series'],
            $data['main']['smsSend'],
            0,
            $data['main']['pcode'],
            $data['main']['idCard']
		);
		// echo $r;
		// exit;
		if($r>=100000){
			/*附属产品下单*/
			if($data['smsSend']==1){
                if(count($data['c_infos'])>0){
                    $this->subOrder($data['c_infos'],$r);
                }
				if($data['reSendMsg']==1) {
                    //最后重发短信
                    $s->reSend_SMS_Global_PL($r,$data['main']['sapply_did'],$data['main']['member']);
                }
			}
			/*记录订单额外信息*/
			$this->appendInfo($r,$data['option']);
			return $r;
		}else{
			$this->errHandle("下单接口出错",$r);
		}
    }
	    
	
}

/**
 *分销下单类
 *分销商分销
 */
class DisOrderC extends OrderSubmitC{
	public function __construct($s,$p,$m){
		parent::__construct($s,$p,$m,0,0);
	}
}

/**
 *零售下单类
 *产品以零售价销售，并分润给分销商，需第三方支付平台支持
 */
class SaleOrderC extends OrderSubmitC{
	public function __construct($s,$p,$m){
		parent::__construct($s,$p,$m,1,1);
	}
}
 
 
?>